<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * @version $Id: inc.positionstool.php 1126 2008-06-09 05:45:04Z julienv $
 * @package Joomleague
 * @copyright Copyright (C) 2007  JoomLeague
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see _joomleague_license.txt
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 
  function jl_save_positions($positions, $project_id, $act) {
    global $database;
    $query = "SELECT pos.id 
                FROM #__joomleague_positions pos, #__joomleague_pos_joomleague posj
               WHERE pos.id = posj.position_id AND posj.project_id = ".$project_id;
              
      $database->setQuery($query);
      
      $arr1 = $database->loadResultArray(); //old
      $arr2 = explode(",",$positions); //new
      
      $diff1 = array_diff($arr1,$arr2); // remove these Items
      $diff2 = array_diff($arr2,$arr1); // insert these Items
      
      #remove
      if(count($diff1) > 0){
                
        $list = sprintf("(%s)", implode(",",$diff1));
        $database->setQuery(sprintf("DELETE FROM #__joomleague_pos_joomleague WHERE project_id = %s AND position_id in %s" , $project_id, $list));
        $database->query();
      }
      #insert
      if(count($diff2) > 0){
        $query = "";
        foreach($diff2 as $v){
            $query .= sprintf("insert into #__joomleague_pos_joomleague (project_id,position_id) values (%s,%s);\n",$project_id, $v);
        }
        
        $database->setQuery($query);

        $database->query_batch(false);

      } 
    }
      
    
    function  jl_assign_positions () {
      global $database, $project;
        $assigned_positions = $project->getPositions();
        //we dont need $assigned_positios in the first list
        $ids = array();
        foreach($assigned_positions as $pos){
          $ids[] = $pos->id;
        }
        
        $v = (count($ids) > 0)? sprintf("AND id not in (%s)", implode(",",$ids)) : "";
        $database->setQuery( "SELECT id, name 
                                FROM #__joomleague_positions
                                WHERE isStaff = 0
                                $v 
                               ORDER BY ordering");
        $results = $database->loadObjectList();
        
        $poslist1 = array();
        foreach($results as $result){
            $poslist1[] = mosHTML::makeOption($result->id, $result->name); 
        }      
        $poslist2 = array();
        
        
        if(count($assigned_positions) > 0){
          foreach($assigned_positions as $pos){
            $poslist2[] = mosHTML::makeOption($pos->id, $pos->name);
          }
        }
         $view = JL_ADMIN_TEMPLATE_PATH.'/edit_assign_positions.tpl.php';    
        $path=_JOOMLA_JL_LIVE_SITE;
        require JL_ADMIN_TEMPLATE_PATH.'/view.tpl.php'; 
    }
?>